package com.wordplat.quickstart.mvp;

import com.wordplat.ikvstockchart.entry.Entry;
import com.wordplat.quickstart.bean.KLineBean;
import com.wordplat.quickstart.utils.ToastViewDialog;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.app.dispatcher.TaskDispatcher;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>StockPresenter</p>
 * <p>Date: 2017/4/11</p>
 *
 * @author afon
 * @since 2017-04-11
 */
public class StockPresenter extends BasePresenter<LoadingView> {

    /**
     * 日 K 一次加载多少天
     */
    private static final int LOAD_BETWEEN_DAYS = 100;
    /**
     * 周 K 一次加载多少周
     */
    private static final int LOAD_BETWEEN_WEEK = 50;
    /**
     * 月 K 一次加载多少个月
     */
    private static final int LOAD_BETWEEN_MONTH = 50;

    private final Calendar currentBeginTime = Calendar.getInstance();
    private final Calendar currentEndTime = Calendar.getInstance();

    private List<KLineBean> kLineList;

    /**
     * KLineType
     */
    public enum KLineType {
        /**
         * DAY ，WEEK ，MONTH
         */
        DAY, WEEK, MONTH
    }

    /**
     * loadFirst
     *
     * @param reqeustCode
     * @param stockCode
     * @param kLineType
     */
    public void loadFirst(int reqeustCode, String stockCode, KLineType kLineType) {
        currentEndTime.setTime(new Date());
        currentBeginTime.setTime(currentEndTime.getTime());

        loadPrev(reqeustCode, stockCode, kLineType);
    }

    /**
     * loadPrev
     *
     * @param requestCode
     * @param stockCode
     * @param kLineType
     */
    public void loadPrev(final int requestCode, String stockCode, final KLineType kLineType) {
        baseView.onStartRequest(requestCode);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentBeginTime.getTime());
        computeNextTime(calendar, kLineType, true);

        baseView.onFinishRequest(requestCode);
        handleError(requestCode, new Throwable("服务器出错"));
    }

    /**
     * loadNext
     *
     * @param requestCode
     * @param stockCode
     * @param kLineType
     */
    public void loadNext(final int requestCode, String stockCode, final KLineType kLineType) {
        baseView.onStartRequest(requestCode);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentEndTime.getTime());
        computeNextTime(calendar, kLineType, false);

        baseView.onFinishRequest(requestCode);
        handleError(requestCode, new Throwable("服务器出错"));
    }

    /**
     * getkLineList
     *
     * @return KLineBean
     */
    public List<KLineBean> getkLineList() {
        return kLineList;
    }

    /**
     * getKLineType
     *
     * @param kLineType
     * @return String
     */
    private String getKLineType(KLineType kLineType) {
        String type = "d";
        switch (kLineType) {
            case DAY:
                type = "D";
                break;

            case WEEK:
                type = "W";
                break;

            case MONTH:
                type = "M";
                break;
            default:
                break;
        }

        return type;
    }

    private void computeNextTime(Calendar calendar, KLineType KlineType, boolean reverse) {
        switch (KlineType) {
            case DAY:
                calendar.add(Calendar.DAY_OF_MONTH, reverse ? -LOAD_BETWEEN_DAYS : LOAD_BETWEEN_DAYS);
                break;

            case WEEK:
                calendar.add(Calendar.WEEK_OF_MONTH, reverse ? -LOAD_BETWEEN_WEEK : LOAD_BETWEEN_WEEK);
                break;

            case MONTH:
                calendar.add(Calendar.MONTH, reverse ? -LOAD_BETWEEN_MONTH : LOAD_BETWEEN_MONTH);
                break;
            default:
                break;
        }
    }
}
